﻿Public Class Customers
    Public Shared KlantenDataSet As DataSet = New DataSet
    Dim DatabaseFuncs As New DatabaseFuncs

    Public _Id As Integer
    Public ReadOnly Property Id() As Integer
        Get
            Id = _Id
        End Get
    End Property

    Private _voorletter As String
    Public Property voorletter() As String
        Get
            voorletter = _voorletter
        End Get
        Set(ByVal value As String)
            _voorletter = value
        End Set
    End Property

    Private _achternaam As String
    Public Property achternaam() As String
        Get
            achternaam = _achternaam
        End Get
        Set(ByVal value As String)
            _achternaam = value
        End Set
    End Property

    Private _ovnummer As String
    Public Property ovnummer() As String
        Get
            ovnummer = _ovnummer
        End Get
        Set(ByVal value As String)
            _ovnummer = value
        End Set
    End Property

    Private _telefoon As String
    Public Property telefoon() As String
        Get
            telefoon = _telefoon
        End Get
        Set(ByVal value As String)
            _telefoon = value
        End Set
    End Property

    Private _email As String
    Public Property email() As String
        Get
            email = _email
        End Get
        Set(ByVal value As String)
            _email = value
        End Set
    End Property

    Private _wachtwoord As String
    Public Property wachtwoord() As String
        Get
            wachtwoord = _wachtwoord
        End Get
        Set(ByVal value As String)
            _wachtwoord = value
        End Set
    End Property


    Public Sub LoadFromDB(ByVal kid As String)
        Try
            Dim queryresult As String = ""
            'User ophalen d.m.v. ovnummer.
            queryresult = DatabaseFuncs.query("SELECT * FROM `klanten` WHERE `id`=" + kid.ToString, 1)
            If Not queryresult = "True" Then 'De query heeft een fout.
                MsgBox("Laden informatie uit Klantendatabase is mislukt (fout vanuit query): " + queryresult)
                Exit Sub
            End If

            Dim DR As DataRow = KlantenDataSet.Tables("Table").Rows(0)
            voorletter = DR.Item("voorletter").ToString
            achternaam = DR.Item("achternaam").ToString
            telefoon = DR.Item("telefoon").ToString
            email = DR.Item("email").ToString
            ovnummer = DR.Item("ovnummer").ToString
            wachtwoord = DR.Item("wachtwoord").ToString
            _Id = DR.Item("id").ToString
        Catch ex As Exception
            MsgBox("Laden informatie uit Klantendatabase is mislukt (fout bij variabelen): " + ex.Message)
        End Try
    End Sub

    Public Function LoadAllFromDB()
        Dim queryresult As String = DatabaseFuncs.query("SELECT * FROM `klanten` ORDER BY `id`", 1)
        If Not queryresult = "True" Then
            Return queryresult
        End If
        Return "True"
    End Function

    Public Sub clearData()
        _Id = Nothing
        voorletter = Nothing
        ovnummer = Nothing
        achternaam = Nothing
        wachtwoord = Nothing
        email = Nothing
        telefoon = Nothing
    End Sub

    Public Sub SaveToDB(ByVal id As Integer, ByVal NieuweUser As Boolean)
        If NieuweUser = True Then
            DatabaseFuncs.query("INSERT INTO `klanten` (voorletter, achternaam, ovnummer, email, telefoon, wachtwoord) VALUES ('" _
            + voorletter + "', '" + achternaam + "', '" + ovnummer + "', '" + email + "', " + telefoon + ", '" + wachtwoord + "')", 1)
        Else
            DatabaseFuncs.query("UPDATE `klanten` SET `voorletter` = '" + voorletter + "', `achternaam` = '" + achternaam + "', " _
            + "`ovnummer` = '" + ovnummer + "', `email` = '" + email + "', `telefoon` = " + telefoon + ", `wachtwoord`='" + wachtwoord + "' WHERE `id`=" + id.ToString, 1)
        End If
    End Sub

    Public Function DeleteFromDB(ByVal id As Integer, Optional ByVal removeIncidents As Boolean = True)
        Dim customers As New Customers
        customers.LoadFromDB(id)
        Dim queryresult As String = DatabaseFuncs.query("DELETE FROM `klanten` WHERE `id`=" + id.ToString, 1)
        If Not queryresult = "True" Then
            Return queryresult
        End If

        If removeIncidents Then
            Dim queryresult2 As String = DatabaseFuncs.query("DELETE FROM `incidenten` WHERE `ovnummer`=" + ovnummer.ToString, 1)
            If Not queryresult2 = "True" Then
                Return queryresult2
            End If
        End If
        clearData()
        Return "True"
    End Function
End Class
